package com.sky.controller.admin;


import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",key = "#setmealDTO.categoryId")
    @ApiOperation("新增套餐")
    @PostMapping
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO){

        setmealService.addSetmeal(setmealDTO);

        return Result.success();
    }


    /**
     * 分页查询套餐信息
     * @param pageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("条件分页查询")
    public Result findSetmeal(SetmealPageQueryDTO pageQueryDTO){

        PageResult pageResult=setmealService.findSetmeal(pageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 根据套餐id查询套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据套餐id查询套餐信息")
    public Result findById(@PathVariable Long id){

        SetmealVO setmealVO=setmealService.findById(id);

        return Result.success(setmealVO);
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("修改套餐信息")
    @PutMapping
    public Result change(@RequestBody SetmealDTO setmealDTO){

        setmealService.change(setmealDTO);

        return Result.success();

    }

    /**
     * 改变套餐起售状态
     * @param status
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("改变套餐起售状态")
    @PostMapping("/status/{status}")
    public Result changeStatus(@PathVariable Integer status,Long id){
        setmealService.changeStatus(status,id);

        return Result.success();
    }


    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "setmeal",allEntries = true )
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    public Result drop(@RequestParam List<Long> ids){

        setmealService.drop(ids);

        return Result.success();

    }
}
